//
//  DetailsListViewController.m
//  Comma
//
//  Created by dlios on 15-1-13.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "DetailsListViewController.h"
#import "DetailsListView.h"
#import "CustomTableViewCell.h"
#import "OnlyImageTableViewCell.h"
#import "OnlyTextTableViewCell.h"
#import "TextAndImageTableViewCell.h"
#import "NoTextAndImageTableViewCell.h"
#import "AFNetWorkingYU.h"
#import "DetailsListModel.h"
#import "UIImageView+WebCache.h"
#import "GlobalMethod.h"
#import "URLMacro.h"
#import "HiddenView.h"
#import "DetailViewController.h"
#import "CategoryViewController.h"
@interface DetailsListViewController ()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation DetailsListViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tableArray = [NSMutableArray array];
        self.category = nil;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.detailsListView = [[DetailsListView alloc] initWithLeftButton:@"类别" RightButton:@"定向" Title:nil FooterView:FooterViewStateNormal];
    [self.view addSubview:_detailsListView];
    [_detailsListView creatView];
    [self startConnect];
    self.detailsListView.tableView.delegate = self;
    self.detailsListView.tableView.dataSource = self;
    
    [self.detailsListView.hiddenView.countryButton addTarget:self action:@selector(areaButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.detailsListView.hiddenView.provinceButton addTarget:self action:@selector(areaButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.detailsListView.hiddenView.cityButton addTarget:self action:@selector(areaButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.detailsListView.hiddenView.schoolButton addTarget:self action:@selector(areaButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.detailsListView.leftButton addTarget:self action:@selector(leftButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)leftButtonAction:(UIButton *)sender
{
    CategoryViewController *categoryView = [[CategoryViewController alloc] init];
    [self.navigationController pushViewController:categoryView animated:YES];
}

- (void)areaButtonAction:(UIButton *)sender
{
    [self.tableArray removeAllObjects];
    NSString *body;
        if ([sender.titleLabel.text isEqualToString:@"全国"]) {
            body = nil;
        } else if ([sender.titleLabel.text isEqualToString:@"本省"]) {
            body = [NSString stringWithFormat:@"areaColumn=%@&areaKey=mine", @"辽宁省"];
        } else if ([sender.titleLabel.text isEqualToString:@"本市"]) {
            body = [NSString stringWithFormat:@"areaColumn=city&areaKey=mine"];
        } else if ([sender.titleLabel.text isEqualToString:@"本校"]) {
            body = [NSString stringWithFormat:@"areaColumn=school&areaKey=mine"];
        }
        [AFNetWorkingYU netWorkWithURL:URL_GET_INFO_LIST body:body resultBlock:^(id result) {
            NSDictionary *dic = [NSDictionary dictionaryWithDictionary:result];
            NSArray *dataArray = [dic objectForKey:@"rows"];
            for (NSDictionary *myDic in dataArray) {
                self.listModel = [[DetailsListModel alloc] initWithDictionary:myDic];
                
                [self.tableArray addObject:self.listModel];
            }
            [self.detailsListView.tableView reloadData];
        }];
}


- (void)startConnect
{
    NSString *url = [NSString stringWithFormat:@"%@?page=1&rows=100", URL_GET_INFO_LIST];
    [AFNetWorkingYU netWorkingHanderGetGataWithUrl:url resultBlock:^(id result) {
        NSDictionary *dic = [NSDictionary dictionaryWithDictionary:result];
        NSArray *dataArray = [dic objectForKey:@"rows"];
        for (NSDictionary *myDic in dataArray) {
            self.listModel = [[DetailsListModel alloc] initWithDictionary:myDic];
            [self.tableArray addObject:self.listModel];
        }
        [self.detailsListView.tableView reloadData];
    }];
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailsListModel *model = [self.tableArray objectAtIndex:indexPath.row];
    if ([model.category isEqualToString:@"视频"] || [model.category isEqualToString:@"公告"]) {
        return 70.0f;
    } else if ([model.content isEqualToString:@""] && model.img != nil) {
        return 160.0f;
    } else if (model.img == nil && ![model.content isEqualToString:@""]) {
        return 128.0f;
    } else if (model.img != nil && ![model.content isEqualToString:@""]) {
        return 192.0f;
    } else {
        return 128.0f;
    }
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.tableArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailsListModel *model = [self.tableArray objectAtIndex:indexPath.row];
    if ([model.category isEqualToString:@"视频"] || [model.category isEqualToString:@"公告"]) {
        CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"customCell"];
        // 获取头像url
        NSString *imgUrl = [NSString stringWithFormat:@"http://www.isu.so/attached/u_img/%@", model.puberImg];
        [cell.headerImage sd_setImageWithURL:[NSURL URLWithString:imgUrl]];
        // 获取标题
        [cell.titleLabel setText:model.title];
        // 获取认证信息
        NSString *puberInfo = [GlobalMethod puberNickChangeString:model.puberRole];
        [cell.infoLabel setText:puberInfo];
        NSString *school = [NSString stringWithFormat:@"·%@",model.puberSchool];
        [cell.puberSchool setText:school];
        [cell.timeLabel setText:model.pubTime];
        [cell.nameLabel setText:model.puberNick];
        [cell.styleLabel setText:model.category];
        return cell;
    } else if (model.img != nil && [model.content isEqualToString:@""]) {
        OnlyImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"imageCell"];
        
        // 获取头像url
        NSString *imgUrl = [NSString stringWithFormat:@"http://www.isu.so/attached/u_img/%@", model.puberImg];
        [cell.headerImage sd_setImageWithURL:[NSURL URLWithString:imgUrl]];
        // 获取标题
        [cell.titleLabel setText:model.title];
        // 获取认证信息
        NSString *puberInfo = [GlobalMethod puberNickChangeString:model.puberRole];
        [cell.infoLabel setText:puberInfo];
        NSString *school = [NSString stringWithFormat:@"·%@",model.puberSchool];
        [cell.puberSchool setText:school];
        [cell.timeLabel setText:model.pubTime];
        [cell.nameLabel setText:model.puberNick];
        [cell.styleLabel setText:model.category];
        [cell.infoImage sd_setImageWithURL:[NSURL URLWithString:model.img]];
        return cell;
    } else if (model.img == nil && ![model.content isEqualToString:@""]) {
        OnlyTextTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"textCell"];
        // 获取头像url
        NSString *imgUrl = [NSString stringWithFormat:@"http://www.isu.so/attached/u_img/%@", model.puberImg];
        [cell.headerImage sd_setImageWithURL:[NSURL URLWithString:imgUrl]];
        // 获取标题
        [cell.titleLabel setText:model.title];
        // 获取认证信息
        NSString *puberInfo = [GlobalMethod puberNickChangeString:model.puberRole];
        [cell.infoLabel setText:puberInfo];
        NSString *school = [NSString stringWithFormat:@"·%@",model.puberSchool];
        [cell.infoTextLabel setText:model.content];
        [cell.puberSchool setText:school];
        [cell.timeLabel setText:model.pubTime];
        [cell.nameLabel setText:model.puberNick];
        [cell.styleLabel setText:model.category];
        return cell;
    } else if (model.img != nil && ![model.content isEqualToString:@""]) {
        TextAndImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"textAndImageCell"];
        
        // 获取头像url
        NSString *imgUrl = [NSString stringWithFormat:@"http://www.isu.so/attached/u_img/%@", model.puberImg];
        [cell.headerImage sd_setImageWithURL:[NSURL URLWithString:imgUrl]];
        // 获取标题
        [cell.titleLabel setText:model.title];
        // 获取认证信息
        NSString *puberInfo = [GlobalMethod puberNickChangeString:model.puberRole];
        [cell.infoLabel setText:puberInfo];
        NSString *school = [NSString stringWithFormat:@"·%@",model.puberSchool];
        [cell.puberSchool setText:school];
        [cell.timeLabel setText:model.pubTime];
        [cell.nameLabel setText:model.puberNick];
        [cell.styleLabel setText:model.category];
        
        [cell.infoTextLabel setText:model.content];
        [cell.infoImage sd_setImageWithURL:[NSURL URLWithString:model.img]];
        return cell;
    }
    else
    {
        OnlyTextTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"textCell"];
        // 获取头像url
        NSString *imgUrl = [NSString stringWithFormat:@"http://www.isu.so/attached/u_img/%@", model.puberImg];
        [cell.headerImage sd_setImageWithURL:[NSURL URLWithString:imgUrl]];
        // 获取标题
        [cell.titleLabel setText:model.title];
        // 获取认证信息
        NSString *puberInfo = [GlobalMethod puberNickChangeString:model.puberRole];
        [cell.infoLabel setText:puberInfo];
        NSString *school = [NSString stringWithFormat:@"·%@",model.puberSchool];
        [cell.infoTextLabel setText:model.content];
        [cell.puberSchool setText:school];
        [cell.timeLabel setText:model.pubTime];
        [cell.nameLabel setText:model.puberNick];
        [cell.styleLabel setText:model.category];
        return cell;
    }
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailViewController *detail = [[DetailViewController alloc] init];
    DetailsListModel *model = [_tableArray objectAtIndex:indexPath.row];
    detail.puberId = model.id;
    [self.navigationController pushViewController:detail animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
